const axios = require('axios')

exports.apiGetQuestionClassify = function () {
  return new Promise((resolve, reject) => {
    axios.post('https://3623f8fd-8d3f-4d06-8034-59bc7addf8f7.bspapp.com/api/femap', {
      action: 'list',
      page: 1
    }).then(res => {
      if (res.data.errcode === 0) {
        resolve(res.data.tags)
      } else {
        reject(res)
      }
    }).catch(reject)
  })
}

exports.apiGetQuestionPage = function (tagid, page) {
  return new Promise((resolve, reject) => {
    axios.post('https://3623f8fd-8d3f-4d06-8034-59bc7addf8f7.bspapp.com/api/femap', {
      action: 'list',
      page,
      tagid
    }).then(res => {
      if (res.data.errcode === 0) {
        resolve({
          list: res.data.data,
          hasNext: res.data.hasNext
        })
      } else {
        reject(res)
      }
    }).catch(reject)
  })
}

exports.apiGetQuestionDetail = function (id, tagid) {
  return new Promise((resolve, reject) => {
    axios.post('https://3623f8fd-8d3f-4d06-8034-59bc7addf8f7.bspapp.com/api/femap', {
      action: 'detail',
      id,
      tagid
    }).then(res => {
      if (res.data.errcode === 0) {
        const { data, next, prev } = res.data
        resolve({
          ...data,
          next,
          prev
        })
      } else {
        reject(res)
      }
    }).catch(reject)
  })
}
